package com.cardbaobao.cardbabyclient.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.model.UserBindCards;
import com.cardbaobao.cardbabyclient.model.result.UserBindCardsResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.base.MyFragmentPagerAdapter;
import com.cardbaobao.cardbabyclient.ui.fragment.card.CreditCardFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.card.DebitCardFragment;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/13 20:00.
 */

public class BankCardManageActivity extends BaseActivity {
    public static final int POSITION_DEBIT_CARD = 0;//储蓄卡
    public static final int POSITION_CREDIT_CARD = 1;//信用卡
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_debit_card)
    TextView tvDebitCard;
    @BindView(R.id.divider_debit_card)
    View dividerDebitCard;
    @BindView(R.id.tv_credit_card)
    TextView tvCreditCard;
    @BindView(R.id.divider_credit_card)
    View dividerCreditCard;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    List<BaseFragment> fragments;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bank_card_manage;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        fragments = new ArrayList<>();
        DebitCardFragment debitCardFragment=new DebitCardFragment();
        debitCardFragment.setBuriedData(mPrePageId);
        fragments.add(debitCardFragment);
        CreditCardFragment creditCardFragment=new CreditCardFragment();
        creditCardFragment.setBuriedData(mPrePageId);
        fragments.add(creditCardFragment);
        MyFragmentPagerAdapter adapter =
                new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentTitleNavigation(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setAdapter(adapter);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        setCurrentTitleNavigation(POSITION_DEBIT_CARD);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
        if (viewPager.getCurrentItem() == POSITION_DEBIT_CARD) {
            GoogleAnalyticsUtil.setScreenName("银行卡-储蓄卡");
        } else {
            GoogleAnalyticsUtil.setScreenName("银行卡-信用卡");
        }
    }

    @OnClick({R.id.ll_debit_card, R.id.ll_credit_card})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_debit_card:
                viewPager.setCurrentItem(POSITION_DEBIT_CARD);
                break;
            case R.id.ll_credit_card:
                viewPager.setCurrentItem(POSITION_CREDIT_CARD);
                break;
        }
    }


    boolean loading;

    public void loadData() {
        if (loading) {
            return;
        }
        loading = true;
        showProgressDialog(false);
        Api.getUserBindCards(String.valueOf(mApplication.getUser().getID())).
                subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {
                        loading = false;
                        dismissProgressDialog();
                        ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                getString(R.string.string_request_data_fail));
                    }

                    @Override
                    public void onNext(String s) {
                        loading = false;
                        dismissProgressDialog();
                        UserBindCardsResult result = JsonUtil.json2Object(s, UserBindCardsResult.class);
                        if (result.getCode() == Constant.SUC_CODE) {
                            UserBindCards cards = result.getData();
                            if (cards != null) {
                                //储蓄卡
                                List<UserBindCards.DebitCard> debitCards = cards.getDebitCardList();
                                ((DebitCardFragment) (fragments.get(POSITION_DEBIT_CARD))).setData(debitCards);
                                //信用卡
                                List<UserBindCards.CreditCard> creditCards = cards.getCreditCardList();
                                ((CreditCardFragment) (fragments.get(POSITION_CREDIT_CARD))).setData(creditCards);
                            }
                        } else {
                            ToastUtil.showToast(result.getMsg());
                        }
                    }
                });
    }

    private void setCurrentTitleNavigation(int position) {
        if (position == POSITION_DEBIT_CARD) {
            tvDebitCard.setAlpha(1.0f);
            dividerDebitCard.setVisibility(View.VISIBLE);
            tvCreditCard.setAlpha(0.5f);
            dividerCreditCard.setVisibility(View.INVISIBLE);
            GoogleAnalyticsUtil.setScreenName("银行卡-储蓄卡");
            mCurrentPageId= Self.Page.app_debit_card_page;
        } else {
            tvCreditCard.setAlpha(1.0f);
            dividerCreditCard.setVisibility(View.VISIBLE);
            tvDebitCard.setAlpha(0.5f);
            dividerDebitCard.setVisibility(View.INVISIBLE);
            GoogleAnalyticsUtil.setScreenName("银行卡-信用卡");
            mCurrentPageId=Self.Page.app_credit_card_page;
        }
    }

    public void onClick(String clickId,String clickDes,String currentPage){
        SelfUtils.buriedClickData(clickId,clickDes,mEnterPageTime,currentPage,mPrePageId);
    }
}
